package com.agileboot.attendance.domain.order.model;

import com.agileboot.attendance.domain.order.db.AttOrderService;
import com.agileboot.attendance.domain.order.entity.AttOrderEntity;
import com.agileboot.attendance.domain.project.db.service.AttProjectService;
import com.agileboot.attendance.domain.supplier.db.AttSupplierService;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 订单模型工厂
 *
 * @author agileboot
 */
@Component
@RequiredArgsConstructor
public class OrderModelFactory {

    private final AttOrderService orderService;
    private final AttProjectService projectService;
    private final AttSupplierService supplierService;

    /**
     * 创建空模型
     *
     * @return 订单模型
     */
    public OrderModel create() {
        return new OrderModel();
    }

    /**
     * 根据ID获取模型
     *
     * @param orderId 订单ID
     * @return 订单模型
     */
    public OrderModel loadById(Long orderId) {
        AttOrderEntity entity = orderService.getById(orderId);
        if (entity == null) {
            throw new ApiException(ErrorCode.Business.COMMON_OBJECT_NOT_FOUND, orderId, "订单");
        }
        OrderModel model = OrderModel.fromEntity(entity);
        
        // 加载关联数据
        model.setProjectEntity(projectService.getById(entity.getProjectId()));
        model.setSupplierEntity(supplierService.getById(entity.getSupplierId()));
        
        return model;
    }
} 